# -*- coding: UTF-8 -*-
"""
@author: tgy
@file: with_as.py
@time: 2021/02/02
"""

class Trace:
    ...
    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if not exc_type:
            ...
            print('normal')
        else:
            print(f'发生了异常 {str(exc_type)}')
            # 返回True表示调用处不向外抛出异常，False为抛出异常，默认是None，即为False
            return True

def test():
    ...
    with Trace() as trace:
        ...
        # print('111')
        raise IndexError('xx')


if __name__ == '__main__':
    pass
    test()
